#!/usr/bin/perl -wc

package RFCMail::Util::PackIt::Data;

use strict;
use bytes;


sub new {
    my $class = shift;
    my @self = (0, @_ ? $_[0] : "");
    return (bless \@self, $class);
}


sub data {
    my ($self) = @_;
    return ($self->[1]);
}


sub read {
    my ($self, $need) = @_;
    my $take = substr($self->[1], $self->[0], $need);
    my $got = length($take);
    die "read past end of data"
	if $got < $need;
    $self->[0] += $got;
    return ($take);
}


sub rest {
    my ($self) = @_;
    my $rest = substr($self->[1], $self->[0]);
    $self->[0] = length($self->[1]);
    return ($rest);
}


sub write {
    my ($self, $data) = @_;
    my $len = length($data);
    substr($self->[1], $self->[0], $len, $data);
    $self->[0] += $len;
    return ($self);
}


sub at_eof {
    my ($self) = @_;
    return ($self->[0] >= length($self->[1]));
}

1;
